package com.maoxs.controller;

import com.maoxs.pojo.User;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/user")
@Api(description = "用户增删改查api")
public class TestController {
    private static volatile int i = 0;
    private static final Map<Integer, User> map = Collections.synchronizedMap(new HashMap<Integer, User>());

    static {
        map.put(++i, new User(1, "maoxiansheng", "123456"));
        map.put(++i, new User(1, "happyfish", "123456"));
        map.put(++i, new User(1, "fulin", "123456"));
        map.put(++i, new User(1, "jack", "123456"));
    }

    @ApiOperation(value = "创建用户", notes = "根据User对象创建用户", httpMethod = "POST")
    @PostMapping("/add")
    public User Add(@RequestBody User user) {
        User put = map.put(++i, user);
        return put;
    }

    @ApiOperation(value = "删除用户", notes = "根据url的id来指定删除用户", httpMethod = "DELETE")
    @ApiImplicitParam(name = "id", value = "这个id是用户的id", paramType = "path")
    @DeleteMapping("/delete/{id}")
    public User Delete(@ApiParam(value = "用户id") @PathVariable(value = "id") Integer id) {
        User remove = map.remove(id);
        return remove;
    }

    @ApiOperation(value = "更新信息", notes = "根据url的id来指定更新用户信息", httpMethod = "PUT")
    @PutMapping(value = "user/{id}")
    public User Update(@ApiParam("这个是用户的id") @PathVariable("id") Integer id, @RequestBody User user) {
        User put = map.put(id, user);
        return put;
    }

    @ApiOperation(value = "获取用户列表", notes = "获取用户列表", httpMethod = "GET")
    @GetMapping("/list")
    public Map<Integer, User> List() {
        return map;
    }


    @ApiOperation(value = "查询用户", notes = "查询用户根据id", httpMethod = "GET")
    @GetMapping("/listById/{id}")
    public User listById(@ApiParam("这个是用户的id") @PathVariable("id") Integer id) {
        User user = map.get(id);
        return user;
    }

}
